﻿using System;
using System.Linq;
using System.ComponentModel.Composition;
using BizControl.Library.BusinessObjects.Participants;
using Csla.Xaml;
using Microsoft.Practices.Prism.Regions;
using BizControl.Library.BusinessObjects.Activities;
using BizControl.Presentation.Infrastructure.Helpers;
using Microsoft.Practices.Prism;
using BizControl.Presentation.Infrastructure.Constants;
using System.ComponentModel;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using Csla;
using BizControl.Library.Security;
using BizControl.Library.BusinessObjects.People;
using BizControl.Library.BusinessObjects.BusinessRoles;

namespace BizControl.Presentation.ActivityEditModule.ViewModels
{
  public class DependentActivitiesViewModel : ViewModel<ActivityLinks>, IHeaderInfoProvider<string>
  {
    private readonly DelegateCommand _addChildLinkCommand;
    private readonly DelegateCommand _addNextLinkCommand;

    public DependentActivitiesViewModel(ActivityBO activity)
    {
      ManageObjectLifetime = false;
      Activity = activity;
      _addChildLinkCommand = new DelegateCommand(AddChildLink, CanAddDependent);
      _addNextLinkCommand = new DelegateCommand(AddNextLink, CanAddDependent);
    }

    protected override void OnModelChanged(ActivityLinks oldValue, ActivityLinks newValue)
    {
      base.OnModelChanged(oldValue, newValue);
      RefreshAllProperties();
    }

    private void Refresh()
    {
      DoRefresh(GetDependentActivities);
    }

    private ActivityLinks GetDependentActivities()
    {
      if (Activity != null)
        return Activity.DependentActivities;
      return null;
    }

    private void RefreshAllProperties()
    {
      OnPropertyChanged("IndependentActivities");
      OnPropertyChanged("SelectedActivity");
    }

    #region Handlers

    private bool CanAddDependent()
    {
      return Activity != null && SelectedActivity != null;
    }

    private void AddChildLink()
    {
      if (Model != null && SelectedActivity != null && !SelectedActivity.IsEmpty())
        Model.AddLink(ActivityBO.GetActivity(SelectedActivity.Id), ActivityLinkType.Child);
      OnPropertyChanged("IndependentActivities");
      SelectedActivity = null;
      Activity.UpdateChildActivities();
      Activity.UpdateNextActivities();
    }

    private void AddNextLink()
    {
      if (Model != null && SelectedActivity != null && !SelectedActivity.IsEmpty())
        Model.AddLink(ActivityBO.GetActivity(SelectedActivity.Id), ActivityLinkType.Next);
      OnPropertyChanged("IndependentActivities");
      SelectedActivity = null;
      Activity.UpdateChildActivities();
      Activity.UpdateNextActivities();
    }

    #endregion

    #region Properties

    public ICommand AddChildLinkCommand
    {
      get { return _addChildLinkCommand; }
    }

    public ICommand AddNextLinkCommand
    {
      get { return _addNextLinkCommand; }
    }

    private ActivityBO _activity;
    public ActivityBO Activity
    {
      get { return _activity; }
      set
      {
        if (_activity != value)
        {
          _activity = value;
          OnPropertyChanged("Activity");
          Refresh();
        }
      }
    }

    private IndependentActivityListForActivity _independentActivities;
    public IndependentActivityListForActivity IndependentActivities
    {
      get 
      {
        if (Activity != null)
          _independentActivities = IndependentActivityListForActivity.GetIndependentActivityListForActivity(Activity);
        return _independentActivities;
      }
    }

    private ActivityInfo _selectedActivity;
    public ActivityInfo SelectedActivity
    {
      get { return _selectedActivity; }
      set
      {
        _selectedActivity = value;
        OnPropertyChanged("SelectedActivity");
        _addChildLinkCommand.RaiseCanExecuteChanged();
        _addNextLinkCommand.RaiseCanExecuteChanged();
      }
    }

    #endregion

    #region IHeaderInfoProvider<string> Members

    public string HeaderInfo
    {
      get { return BizControl.Presentation.ActivityEditModule.Properties.Resources.DependentActivitiesHeaderInfo; }
    }

    #endregion

  }

}
